package com.atelier.plugin.database.ui;

import com.atelier.plugin.database.domain.DatabaseConnectionInfo;
import com.intellij.ui.JBColor;

import javax.swing.*;
import java.awt.*;

public class DatabaseConnectionListCellRenderer extends JPanel implements ListCellRenderer<DatabaseConnectionInfo> {
    private JLabel iconLabel;
    private JLabel textLabel;

    public DatabaseConnectionListCellRenderer() {
        setLayout(new BorderLayout());
        setOpaque(true);
        setBackground(JBColor.WHITE); // 设置背景颜色，根据需要更改
        iconLabel = new JLabel();
        textLabel = new JLabel();
        textLabel.setHorizontalAlignment(SwingConstants.LEFT); // 文本左对齐
        iconLabel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 10)); // 可以调整图标和文本之间的间距
        add(iconLabel, BorderLayout.WEST);
        add(textLabel, BorderLayout.CENTER);
    }

    @Override
    public Component getListCellRendererComponent(JList<? extends DatabaseConnectionInfo> list, DatabaseConnectionInfo value, int index, boolean isSelected, boolean cellHasFocus) {
        if (value != null) {
            iconLabel.setIcon(value.getDatabaseType().getIcon());
            textLabel.setText(value.getName());

            if (isSelected) {
                setBackground(Colors.LIGHT_BLUE);

               // setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
               // setForeground(list.getForeground());
            }
        }
        return this;
    }
}
